$(document).ready(function () {
	// LoginRegisterEvent
	$('#modal').on('show.bs.modal', function (event) {
		
		// login
		if ($(event.relatedTarget).attr('data-value') == 'login') {
			$('.modal-content').load('/login', function () {
				
				$('#loginForm').submit(function (event) {
					event.preventDefault();
					var formValues = $(this).serialize();
					$.post('/login', formValues, function (result) {
						if(result.status == 0) {
							swal({
							  title: "登录失败",
							  text: result.data.message,
							  type: "error"
							});
						}else if(result.status == 1) {
							swal({
							  title: "登录成功",
							  text: "欢迎进入 News新闻……",
							  timer: 600,
							  showConfirmButton: false
							});
							
							setTimeout(function(){
							    var oldHref = window.location.href;
							    window.location.href = oldHref;
							}, 600);
						}
					});
				});
				
			});
			
		// register
		} else if($(event.relatedTarget ).attr('data-value') == 'register'){
		  	$('.modal-content').load('/register', function () {
		  		
		  		// 验证事件
		  		$('#registerForm input').blur(function () {
		  			// 昵称
		  			if($(this).attr('name') == "name" ){
		  				$(this).siblings('p.text-danger').remove();
		  				if($(this).val() == ""){
		  					$(this).parent().append('<p class="text-danger">昵称不能为空.</p>');
		  				}
		  			}else if($(this).attr('name') == "account") {
		  				$(this).siblings('p.text-danger').remove();
		  				if($(this).val().length < 6){
		  					$(this).parent().append('<p class="text-danger">账号最少请输入7位数.</p>');
		  				}
		  			}else if($(this).attr('name') == "password") {
		  				$(this).siblings('p.text-danger').remove();
		  				if($(this).val() == ""){
		  					$(this).parent().append('<p class="text-danger">密码不能为空.</p>');
		  				}
		  			}else if($(this).attr('id') == "rePassword") {
		  				$(this).siblings('p.text-danger').remove();
		  				if($(this).val() != $('#password').val()){
		  					$(this).parent().append('<p class="text-danger">两次密码不一样.</p>');
		  				}
		  			}else if($(this).attr('name') == "email" ) {
		  				$(this).siblings('p.text-danger').remove();
		  				if($(this).val() == ""){
		  					$(this).parent().append('<p class="text-danger">邮箱不能为空.</p>');
		  				}
		  			}
		  		});
		  		
		  		// 提交事件
		  		$('#registerForm').submit(function (event) {
					event.preventDefault();
					
					var flag = 0;
					$('#registerForm input').each(function (index, item) {
						if($(this).val() == "") {
							$(this).siblings('p.text-danger').remove();
							var text = $(this).closest('div.form-group').find('label').text();
							$(this).parent().append('<p class="text-danger">' + text + '不能为空.</p>');
							flag = 1;
						}else if($(this).parent().find('p.text-danger').length > 0) {
							flag = 1;
						}
					});
					if(flag) {
						return false;
					}
					var formValues = $(this).serialize();
					$.post('./register', formValues, function (result) {
						if(result.status == 0) {
							swal({
							  title: "注册失败",
							  text: "数据库错误！",
							  type: "error"
							});
						}else if(result.status == 1) {
							swal({
							  title: "注册成功",
							  text: "登录并前往主页吗？",
							  type: "success",
							  showCancelButton: true,
							  confirmButtonColor: "#DD6B55",
							  confirmButtonText: "Yes!",
							  closeOnConfirm: false
							},
							function(isConfirm){
							  if (isConfirm) {
							  	window.location.href="/";
							  } else {
							    window.location.href="/loginOut";
							  }
							});
						}
					});
				});
		  		
		  	});
		}
	});
	
	
	// 搜索功能
	var $searchList = $('#searchList');
	var $search = $('#search');
	$searchList.hide();
	$search.keyup(function () {
		$searchList.empty();
		var data = {
			content: $search.val()
		};
		$.post('/search', data, function (result) {
			if(result.status == 1) {
				result.data.forEach(function (item) {
					var html = "";
					html += '<li class="list-group-item">';
					html += item.title;
					html += '</li>';
					$searchList.append(html);
					$searchList.find('li')
						.hover(function (event) {
							$(event.target).addClass('active');
						}, function (event) {
							$(event.target).removeClass('active');
						})
						.click(function (event) {
							var text = $(event.target).text();
							$search.val(text);
						});
				});
			}
		});
	});
	$search.focus(function () {
		$searchList.fadeIn();
	});
	$search.blur(function () {
		$searchList.fadeOut();
	});
	$('#searchForm').submit(function (event) {
		event.preventDefault();
		
		var content = $search.val();
		var classify = 'all';
		var href = "/search/moreNews?classify=";
		href += classify;
		href += "&content=";
		href += content;
		window.location.href = href;
	});
});
